<template>
    <div>
        <el-select v-model="formInline.city" filterable clearable :size="size" @change="onChange">
            <el-option v-for="(item, index) in doctorList" :key="index" :label="item.name" :value="item.id"></el-option>
        </el-select>
    </div>
</template>
<script>
import { GET_DOCTOR_OPTIONS } from "@/http/doctor";
export default {
    name: "DoctorSelect",
    props: {
        value: {
            type: [String, Number],
            default: "",
        },
        code: {
            type: [String, Number],
            default: "",
        },
        size: {
            type: String,
            default: ''
        },
        multiple: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            doctorList: this.$store.state.doctorList,
            formInline: {},
        };
    },
    watch: {
        value: {
            handler: function (n, o) {
                if (!n) {
                    this.formInline = {};
                    return false;
                }
                // 多选
                if (this.multiple) {
                    this.$set(
                        this.formInline,
                        "city",
                        n.split(",").map((k) => Number(k))
                    );
                } else {
                    this.$set(this.formInline, "city", Number(n));
                }
            },
            immediate: true,
        },
        code: {
            handler: function (n, o) {
                if (n) {
                    this.getList();
                }
            },
        },
    },
    created() {
        // let n = this.$store.state.doctorList;
        // if (!n || n.length <= 0) {
        //     this.getList();
        // }
        this.getList();
    },
    methods: {
        // 获取医院
        getList() {
            let oParams = {};
            if (this.code) {
                oParams.city = this.code;
            }
            GET_DOCTOR_OPTIONS(oParams).then((res) => {
                let list = res.data || [];
                this.doctorList = list;
                this.$store.commit("SET_DOCTOR_LIST", list);
            });
        },

        // 监听组件变化
        onChange(e) {
            console.log(e);
            this.$emit("input", e.toString());
            this.$emit("change", e.toString());
        },
    },
};
</script>
<style lang="scss" scoped>

</style>